<?php

namespace Biz\service\springHomeDecActivity;

use Biz\model\springHomeDecActivity\ActivityProduct;
use Biz\model\springHomeDecActivity\MSpringHomeDecWinningRecord as mWinningRecord;
use Doraemon\service\User;

class WiningService
{
    #滚动数量
    const SCROLL_COUNT = 18;
    const CHUNK_COUNT = 3;

    #活动攻略
    const STRATEGY = [
        0 => '每满50块砖可兑换1张锦鲤券',
        1 => '攻略：获得锦鲤券越多，抽中锦鲤几率越大哦～',
        2 => '秘籍：邀请新用户+50块砖',
    ];

    const MSG_TYPE_WINNING = 1;
    const MSG_TYPE_STRATEGY = 2;


    /**
     * 获取我的奖品
     * @param $uid
     * @return array
     */
    public static function getUserLottery($uid)
    {
        $where['uid'] = $uid;
        $where['data_status'] = mWinningRecord::STATUS_NORMAL;
        $where['ORDER'] = [
            'id' => 'DESC',
        ];
        $userLottery = [];
        $result = mWinningRecord::dataSelect($where);
        if ($result && is_array($result)) {
            $productIds = array_unique(array_column($result, 'product_id'));
            $productMap = WishListService::getProductsMap($productIds);
            foreach ($result as $winningRecord) {
                $lottery = $productMap[$winningRecord['product_id']] ?? [];
                if ($lottery) {
                    $lottery['winning_record_id'] = $winningRecord['id'];
                    $lottery['date'] = $winningRecord['winning_date'];
                    $lottery['is_reserve'] = (int)$winningRecord['is_reserve'];
                    $lottery['delivery'] = [
                        'name' => $winningRecord['name'],
                        'phone' => $winningRecord['phone'],
                        'delivery_address' => $winningRecord['delivery_address'],
                    ];
                    $userLottery[] = $lottery;
                }
            }
        }
        return $userLottery;
    }


    /**
     * 留资发货信息
     * @param $uid
     * @param $recordId
     * @param $name
     * @param $phone
     * @param $address
     * @return bool|int
     */
    public static function setDeliveryInfo($uid, $recordId, $name, $phone, $address = '')
    {
        $where['id'] = $recordId;
        $where['uid'] = $uid;
        $where['data_status'] = mWinningRecord::STATUS_NORMAL;
        $winningRecord = mWinningRecord::dataSelectInfo($where);
        if (!$winningRecord) {
            return false;
        }
        if (mWinningRecord::RESERVE_YES == $winningRecord['is_reserve']) {
            return false;
        }
        if (ActivityProduct::TYPE_PRODUCT == $winningRecord['product_type'] && empty($address)) {
            return false;
        }
        $data['name'] = $name;
        $data['phone'] = $phone;
        $data['delivery_address'] = $address;
        $data['is_reserve'] = mWinningRecord::RESERVE_YES;
        $data['update_time'] = date('Y-m-d H:i:s', time());
        return mWinningRecord::dataUpdate($data, $where);
    }

    /**
     *
     * @return array|void
     */
    public static function scrollWinningInfo()
    {
        $where['data_status'] = mWinningRecord::STATUS_NORMAL;
        $where['ORDER'] = [
            'id' => 'DESC',
        ];
        $where['LIMIT'] = [0, self::SCROLL_COUNT];
        $scrollList = [];

        foreach (self::STRATEGY as $msg){
            $msgItem['msg'] = [
                'type' => self::MSG_TYPE_STRATEGY,
                'content' => $msg,
            ];
            $scrollList[] = $msgItem;
        }

        $records = mWinningRecord::dataSelect($where);
        if (empty($records)) {
            return $scrollList;
        }
        $uids = array_unique(array_column($records, 'uid'));
        $usersInfo = User::getBatchBasicUserInfo($uids);
        $usersInfoMap = array_column($usersInfo, null, 'uid');
        $productIds = array_unique(array_column($records, 'product_id'));
        $productMap = WishListService::getProductsMap($productIds);
        $lastIndex = 0;
        foreach ($records as $key => $record) {
            $msgItem = [];
            $userInfo = $usersInfoMap[$record['uid']];
            $product = $productMap[$record['product_id']];
            $chunkCount = self::CHUNK_COUNT;
            $msgItem['user_info'] = [
                'uid' => $userInfo['uid'],
                'nick' => $userInfo['nick'],
                'avatar' => $userInfo['avatar'],
                'small_avatar' => $userInfo['small_avatar'],
                'big_avatar' => $userInfo['big_avatar'],
            ];
            if ($key > 0 && $key % $chunkCount == 0) {
                $msgItem['msg'] = [
                    'type' => self::MSG_TYPE_STRATEGY,
                    'content' => self::STRATEGY[$lastIndex],
                ];
                $lastIndex = $lastIndex < count(self::STRATEGY) - 1 ? $lastIndex + 1 : 0;
                $scrollList[] = $msgItem;
            }

            $msgItem['msg'] = [
                'type' => self::MSG_TYPE_WINNING,
                'content' => $product['product_name'],

            ];
            $scrollList[] = $msgItem;
        }
        return $scrollList;
    }

}